import 'package:flutter/material.dart';
import 'package:wu_you/res/colors.dart';
import 'package:wu_you/utils/utils.dart';

enum SortType {
  ///顺序（默认排序方式）
  up,

  ///倒序
  down,

  ///没有选择
  none,
}

class SortButton extends StatefulWidget {
  final Widget child;

  final ValueChanged<SortType> onChanged;

  ///初始化排序方式
  final SortType initSortType;

  ///如果排序方式为[SortType.none],那么这个排序将是第一次被点击时候的排序方式
  final SortType firstSelectSortType;

  SortButton({
    Key key,
    @required this.child,
    @required this.onChanged,
    this.initSortType = SortType.none,
    this.firstSelectSortType = SortType.up,
  })  : assert(child != null),
        assert(onChanged != null),
        super(key: key);

  @override
  _SortButtonState createState() => _SortButtonState();
}

class _SortButtonState extends State<SortButton> {
  SortType _sortType;

  @override
  void initState() {
    super.initState();
    this._sortType = widget.initSortType;
  }

  @override
  Widget build(BuildContext context) {
    return TextButton(
      child: Row(
        children: [
          widget.child,
          Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              SizedBox(height: 1,),
              Image.asset(Utils.getImgPath('ic_mall_search_up',directory: "mall"),width: 8,height: 8,color: this._sortType == SortType.up ? AppColors.primaryColor : Colors.black),
              // Divider(color: Colors.transparent, height: 2),
              Image.asset(Utils.getImgPath('ic_mall_search_down',directory: "mall"),width: 8,height: 8,color: this._sortType == SortType.down ? AppColors.primaryColor : Colors.black),
            ],
          ),
        ],
      ),
      onPressed: () {
        switch (_sortType) {
          case SortType.up:
            _sortType = SortType.down;
            break;
          case SortType.down:
            _sortType = SortType.up;
            break;
          case SortType.none:
            _sortType = widget.firstSelectSortType;
            break;
        }
        setState(() {
          widget.onChanged(_sortType);
        });
      },
    );
  }
}
